﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BUS;
using DTO;
using System.Configuration;
using System.Web.Configuration;
using System.Net.Configuration;

namespace FastDeal.Admin
{
    public partial class KhuyenMaiMoi : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
              if (Session["DaDangNhap"] == null || (bool)Session["DaDangNhap"] == false)
              {
                  Response.Redirect("~/Default.aspx");
              }
              if ((int)Session["LoaiThanhVien"] != 1)
              {
                  Response.Redirect("~/Default.aspx");
              }
              if (!IsPostBack)
              {
                  LoadDanhSachKhuyenMai_ChuaDuyet();
              }

              //KhuyenMai_BUS db = new KhuyenMai_BUS();
              //KhuyenMai km = new KhuyenMai();
              //km = db.KhuyenMai_MaKhuyenMai(37);
              //km.TinhTrang = 1;
              //db.Sua_KhuyenMai(km);
        }

        /// <summary>
        /// Load danh sách khuyến mãi chưa duyệt
        /// </summary>
        private void LoadDanhSachKhuyenMai_ChuaDuyet()
        {
            KhuyenMai_BUS db = new KhuyenMai_BUS();
            List<KhuyenMai> list = new List<KhuyenMai>();
            list = db.LayDanhSachTheoTinhTrang_KhuyenMai(0);
            gvDanhSachKhuyenMaiChuaDuyet.DataSource = list;
            gvDanhSachKhuyenMaiChuaDuyet.DataBind();

            if (Panel1.Height.Value < 800)
                Panel1.Height = 800;

            ////Lay ten username
            //int i = 0;
            //foreach (GridViewRow dr in gvDanhSachKhuyenMaiChuaDuyet.Rows)
            //{
            //    //Lay userid o cot thuc 2
            //    int userID = int.Parse(dr.Cells[1].Text.ToString());
            //    TaiKhoan_BUS db1 = new TaiKhoan_BUS();
            //    TaiKhoan tk = new TaiKhoan();
            //    tk = db1.LayThongTin_TaiKhoan(userID);
            //    gvDanhSachKhuyenMaiChuaDuyet.Rows[0].Cells[1].Text = tk.UserName;

            //    gvDanhSachKhuyenMaiChuaDuyet.Rows[i].Cells[2].Text = "Chưa Duyệt";
            //    i++;
            //}
            if (list.Count == 0)
                Panel2.Visible = true;
        }

        protected void gvDanhSachKhuyenMaiChuaDuyet_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvDanhSachKhuyenMaiChuaDuyet.EditIndex = e.NewEditIndex;
            LoadDanhSachKhuyenMai_ChuaDuyet();
        }

        protected void gvDanhSachKhuyenMaiChuaDuyet_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvDanhSachKhuyenMaiChuaDuyet.EditIndex = -1;
            LoadDanhSachKhuyenMai_ChuaDuyet();
        }

        protected void gvDanhSachKhuyenMaiChuaDuyet_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int ma = int.Parse(((Label)gvDanhSachKhuyenMaiChuaDuyet.Rows[e.RowIndex].Cells[0].FindControl("txtMaKhuyenMai")).Text);
            int tinhtrang = int.Parse(((DropDownList)gvDanhSachKhuyenMaiChuaDuyet.Rows[e.RowIndex].Cells[3].FindControl("ddlTinhTrang")).SelectedValue);
            //cập nhật xuống csdl
            KhuyenMai_BUS db = new KhuyenMai_BUS();
            KhuyenMai km = new KhuyenMai();
            km = db.KhuyenMai_MaKhuyenMai(ma);
            km.TinhTrang = tinhtrang;
            db.Sua_KhuyenMai(km);

            gvDanhSachKhuyenMaiChuaDuyet.EditIndex = -1;
            LoadDanhSachKhuyenMai_ChuaDuyet();

            //Thực hiện gửi mail cho các email co trong danh sách
            if (tinhtrang == 1)
            {
                Configuration config = WebConfigurationManager.OpenWebConfiguration("~/Web.config");
                MailSettingsSectionGroup mailSettings = config.GetSectionGroup("system.net/mailSettings") as MailSettingsSectionGroup;
                if (mailSettings != null)
                {
                    int port = mailSettings.Smtp.Network.Port;
                    //string from = mailSettings.Smtp.From;
                    string from = mailSettings.Smtp.Network.UserName;
                    string host = mailSettings.Smtp.Network.Host;
                    string pass = mailSettings.Smtp.Network.Password;
                    string username = mailSettings.Smtp.Network.UserName;

                    System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
                    message.From = new System.Net.Mail.MailAddress(from);
                    message.Subject = "ohMua! Khuyến mãi hot trong ngày - "+ km.TenKhuyenMai;
                    //message.To.Add(new System.Net.Mail.MailAddress("vantamck2@gmail.com"));
                    //Lấy danh sách mail trong csdl
                    Email_KhuyenMai_BUS db6 = new Email_KhuyenMai_BUS();
                    List<Email_KhuyenMai> list6 = new List<Email_KhuyenMai>();
                    list6 = db6.LayDanhSach_Email();
                    foreach (Email_KhuyenMai dr6 in list6)
                    {
                        message.To.Add(new System.Net.Mail.MailAddress(dr6.Email));
                    }

                    message.IsBodyHtml = true;
                    message.Body = "<H4>Khuyến mãi hot trong ngày - oh!Mua</H4><br />" + "Click để xem: <a href='ChiTietKhuyenMai.aspx?ID='"+km.MaKhuyenMai.ToString()+" >Link</a>";
                                       
                    System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
                    client.Host = host;
                    client.Port = port;
                    client.Credentials = new System.Net.NetworkCredential(username, pass);
                    client.EnableSsl = true;
                    try
                    {
                        client.Send(message);
                    }
                    catch (Exception ex)
                    {

                    }
                }
            }
        }

    }
}